<template>
  <div class="app-container">
    <el-alert
      title="⚠️ 卡组"
      type="success"
      :description="description">
    </el-alert>
    <br/>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="唯一id" prop="uid">-->
<!--        <el-input-->
<!--          v-model="queryParams.uid"-->
<!--          placeholder="请输入唯一id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="单元" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单元"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单词" prop="word">
        <el-input
          v-model="queryParams.word"
          placeholder="请输入单词"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="词性" prop="cx">
        <el-input
          v-model="queryParams.cx"
          placeholder="请输入词性"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="释义" prop="sy">
        <el-input
          type="textarea"
          autosize
          placeholder="请输入释义"
          v-model="queryParams.sy">
          @keyup.enter.native="handleQuery"
          clearable
        </el-input>
      </el-form-item>
      <el-form-item label="复习次数" prop="goover">
        <el-input
          v-model="queryParams.goover"
          placeholder="请输入复习次数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="搜索值" prop="searchValue">-->
<!--        <el-input-->
<!--          v-model="queryParams.searchValue"-->
<!--          placeholder="请输入搜索值"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['Goover:English:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['Goover:English:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['Goover:English:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Goover:English:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="3">
        <el-select
          v-model="select1"
          filterable
          default-first-option
          placeholder="选择卡片"
          @change="SelectChange"
        >
          <el-option
            v-for="item in opotionscopy"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="2">
        <el-select
          v-model="select2"
          multiple
          filterable
          default-first-option
          placeholder="选择标签"
          @change="SelectChange"
        >
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="EnglishList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="唯一id" align="center" prop="uid" />-->

      <el-table-column label="单词"  align="left">
        <template slot-scope="scope">
          <i class="el-icon-headset" @click="getaudiofun(scope.row.word)"/>&nbsp;
          &nbsp;<span>{{scope.row.word}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="词性" align="center" prop="cx" />-->
      <el-table-column label="释义"  align="left" prop="sy" />
      <el-table-column label="标签" align="center" prop="searchValue" />
      <el-table-column label="备注"  align="center" prop="remark" />
      <el-table-column label="复习次数" align="center" prop="goover" />
      <el-table-column label="卡片" align="center" prop="unit" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['Goover:English:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['Goover:English:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改Goover对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="id" prop="id">-->
<!--          <el-input v-model="form.id" placeholder="请输入id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="唯一id" prop="uid">-->
<!--          <el-input v-model="form.uid" placeholder="请输入唯一id" />-->
<!--        </el-form-item>-->

        <el-form-item label="卡片" prop="word">
          <el-select
            v-model="form.unit"
            filterable
            default-first-option
            placeholder="选择卡片">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="单词" prop="word">
          <el-input ref="inputt" v-model="form.word"  v-focus placeholder="请输入单词" />
        </el-form-item>
<!--        <el-form-item label="词性" prop="cx">-->
<!--          <el-input v-model="form.cx" placeholder="请输入词性" />-->
<!--        </el-form-item>-->
        <el-form-item label="释义" prop="sy">
          <el-input
            type="textarea"
            autosize
            placeholder="请输入释义"
            v-model="form.sy">
            clearable
          </el-input>
        </el-form-item>
<!--        <el-form-item label="复习次数" prop="goover">-->
<!--          <el-input v-model="form.goover" placeholder="请输入复习次数" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>

        <el-form-item label="标签" prop="searchValue">
          <el-select
            v-model="form.searchValue"
            multiple
            filterable
            allow-create
            default-first-option
            placeholder="添加标签">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">录 入</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import {
  getdescription,
  listEnglish,
  getEnglish,
  delEnglish,
  addEnglish,
  updateEnglish,
  GetLabel
} from "@/api/Goover/English";
import { getCourse,listUnit} from "@/api/Unit/Unit";
import axios from "axios";

export default {
  name: "English",
  data() {
    return {
      description:'',
      select1:'',
      select2:'',
      options:[],  //卡片
      options1:[], //标签
      opotionscopy:[],//卡片的备份
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // Goover表格数据
      EnglishList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pageNum1: 1,
        pageSize1: 10,
        uid: null,
        unit: null,
        word: null,
        cx: null,
        sy: null,
        goover: null,
        searchValue: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      audioParams:{
        type:2,
        audio:"statue"
      }
    };
  },
  created() {
    this.getList();
    this.getUnit();
    this.getLabel();
    this.getdescriptionrun();

  },
  directives: {
    focus:{
      inserted: function (el) {
        el.querySelector('input').focus()
      }
    }
  },
  methods: {
    getaudiofun(row){
      this.audioParams.audio=row;
      axios({
        method: "get",
        url: "/getaudio/dictvoice",
        params: this.audioParams,
        responseType: "blob"
      }).then(res => {
        let url = URL.createObjectURL(res.data);//通过这个API让语音数据转为成一个url地址
        let audio = new Audio();//在VUE中使用audio标签
        audio.src = url;//设置audio的src为上面生成的url
        audio.play();//进行播放
      })

    },
    SelectChange(){
      this.queryParams.pageNum = 1;
      this.queryParams.pageNum1 = 1;
      this.queryParams.unit=this.select1;
      this.getList();
    },
    getdescriptionrun(){
      getdescription().then(res =>{
        this.description=res.course;
      })
    },
    handleChange(val) {
      // val 代表 value 值
      if (val) {
        // 操作选中角色发生变化
        this.$set(this.options, val)
      } else {
        this.$set(this.options, '')
      }
    },
    getLabel(){
      GetLabel().then(res => {
        var y=new Array();
        for(var x of res ){
          y.push({label:x.label,value:x.uid});
        }
        this.options1=y;
      });
    },
    getUnit() {
      listUnit().then(response => {
        var y=new Array();
        for(var x of response.rows ){
          y.push({label:x.unit,value:x.uid});
        }
        this.options=y.slice(0);
        y.push({label:'显示所有卡片',value:''});
        this.opotionscopy=y;
      });
    },
    /** 查询Goover列表 */
    async getList() {
      this.loading = true;
      //翻译课程
      var transalte;
      await getCourse().then(response => {
        transalte=response;
      });
      this.queryParams.pageNum1 = this.queryParams.pageNum;
      this.queryParams.pageSize1 = this.queryParams.pageSize;

      listEnglish(this.queryParams).then(response => {

        //过滤
        // console.log(this.select2)
        // console.log(response.rows)
        //首先判断是否需要过滤
        var tf='';
        if(this.select2.length>0){
          var list=new Array(); //存放过滤后值
          //过滤
          for(const r of response.rows){
            tf='1';//重置过滤器
            for(let e of this.select2){
              if(r.searchValue.indexOf(e)==-1){ //不过滤
                tf='';
              }
            }
            if(tf=='1'){list.push(r);tf='';};
          }
          response.rows=list.slice(0);
        }

        for(var x of response.rows){ //翻译卡组
          var ser='';
          for(const y of transalte){
            if(x.courseuid===y.uid){
              x.courseuid=y.course;
            }
          }

          for(var r of this.options1){  //翻译标签
            for(const z of x.searchValue){
              if(z==r.value){
                ser=ser+"  "+r.label;
              }
            }
          }
          for(const e of this.options){ //翻译卡片
            if(e.value==x.unit){
              x.unit=e.label;
            }
          }
          ser=ser.trim();
          x.searchValue=ser;
        }

        this.EnglishList = response.rows;
        this.total = response.total;
        this.loading = false;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uid: null,
        unit: null,
        word: null,
        cx: null,
        sy: null,
        goover: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        searchValue: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加内容";
      this.$refs.inputt.focus();
      if(this.select1!=""){
        this.form.unit=this.select1;
      }else{
        this.form.unit=this.options[this.options.length-1].value;
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEnglish(id).then(response => {
        this.form = response.data;
        var list=this.form.searchValue.split("|");
        var list2=new Array();
        for(var i=0;i<list.length-1;i++){  //妈的大费周折其实就为了去掉最后一个空的元素
          list2.push(list[i]);
        }
        this.form.searchValue =list2; //去除空的那个元素
        this.open = true;
        this.title = "修改内容";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEnglish(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if(this.form.word==null||this.form.unit==null||this.form.sy==null){
              this.$message({
                message:"创建失败 - 请输入完整的内容后在创建！",
                type:"error"
              })
              this.open = false;
              return;
            }

            addEnglish(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.form.word=null;
              this.form.sy=null;
              this.form.remark=null;
              this.getList();
              this.$refs.inputt.focus();

            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除内容【' + row.word + '】的数据？').then(function() {
        return delEnglish(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('Goover/English/export', {
        ...this.queryParams
      }, `English_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
